home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / applet / overview / example / SimpleClick.java < prev   
Encoding:
Java Source  |  1997-07-13  |  1.7 KB  |  61 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.Applet;
  18. import java.awt.Graphics;
  19. import java.awt.Event;
  20.  
  21. public class SimpleClick extends Applet {
  22.  
  23.     StringBuffer buffer;
  24.  
  25.     public void init() {
  26.     buffer = new StringBuffer();
  27.         addItem("initializing... ");
  28.     }
  29.  
  30.     public void start() {
  31.         addItem("starting... ");
  32.     }
  33.  
  34.     public void stop() {
  35.         addItem("stopping... ");
  36.     }
  37.  
  38.     public void destroy() {
  39.         addItem("preparing for unloading...");
  40.     }
  41.  
  42.     void addItem(String newWord) {
  43.         System.out.println(newWord);
  44.         buffer.append(newWord);
  45.         repaint();
  46.     }
  47.  
  48.     public void paint(Graphics g) {
  49.     //Draw a Rectangle around the applet's display area.
  50.         g.drawRect(0, 0, size().width - 1, size().height - 1);
  51.  
  52.     //Draw the current string inside the rectangle.
  53.         g.drawString(buffer.toString(), 5, 15);
  54.     }
  55.  
  56.     public boolean mouseDown(Event event, int x, int y) {
  57.     addItem("click!... ");
  58.     return true;
  59.     }
  60. }
  61.